<?php
namespace app\index\controller;
use app\index\model\Users as M;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 会员控制器
 */
class Users extends Base{
    
    public function index(){
        $classifyList = model('classify')->field('classifyId,classifyName')->where(['dataFlag'=>1])->order('classifySort asc, classifyId asc')->select();
        $this->assign("classifyList",$classifyList);
        $diseaseList = model('disease')->field('diseaseId,diseaseName')->where(['dataFlag'=>1])->order('diseaseSort asc, diseaseId asc')->select();
        $this->assign("diseaseList",$diseaseList);
        $this->assign("staffRoleId",session("HTR_STAFF.staffRoleId"));
        return $this->fetch("list");
    }
    /**
     * 获取分页
     */
    public function pageQuery(){
        $m = new M();
        return $m->pageQuery();
    }
    /**
     * 跳去编辑页面
     */
    public function toEdit(){
        $classifyList = model('classify')->field('classifyId,classifyName')->where(['dataFlag'=>1])->order('classifySort asc, classifyId asc')->select();
        $this->assign("classifyList",$classifyList);
        $interestList = model('interest')->field('interestId,interestName')->where(['dataFlag'=>1])->order('interestSort asc, interestId asc')->select();
        $this->assign("interestList",$interestList);
        $m = new M();
        $data = $this->get();
        $diseaseList = model('disease')->field('diseaseId,diseaseName')->where(['dataFlag'=>1])->order('diseaseSort asc, diseaseId asc')->select();
        $this->assign("diseaseList",$diseaseList);
        $this->assign("areaList",model('areas')->listQuery(0));
        if(session("HTR_STAFF.staffRoleId")==2){
            $this->assign("staffList",model('Staffs')->where(['dataFlag'=>1,'staffRoleId'=>3,'shopId'=>session("HTR_STAFF.shopId")])->column('staffName','staffId'));
        }else{
            $this->assign("staffList",model('Staffs')->where(['dataFlag'=>1,'staffRoleId'=>3])->column('staffName','staffId'));
        }
        $assign = ['data'=>$data];
        return $this->fetch("edit",$assign);
    }
    /*
    * 获取数据
    */
    public function get(){
        $m = new M();
        return $m->getById((int)Input("id"));
    }
    /**
     * 新增
     */
    public function add(){
        $m = new M();
        return $m->add();
    }
    /**
    * 修改
    */
    public function edit(){
        $m = new M();
        return $m->edit();
    }
    /**
     * 删除
     */
    public function del(){
        return model('users')->del();
    }
    /**
     * 判断手机号是否存在
     */
    public function checkPhone(){
        $m = new M();
        $rs = $m->checkPhone();
        if($rs['status']==1){
            return ['ok'=>$rs['msg']];
        }else{
            return ['error'=>$rs['msg']];
        }
    }
    /**
     * 柱状图
     */
    public function statistics(){
        $year = Input("Y");
        if(!$year)$year = date('Y');
        $lyear = $year-1;
        $ryear = $year+1;
        $this->assign("year",$year);
        $this->assign("lyear",$lyear);
        $this->assign("ryear",$ryear);
        $type0 = $type1 = $type2 = '';
        for($i=1;$i<13;$i++){
            $start = $year.'-'.$i.'-1';
            if($i == 12){
                $end = ($year+1).'-1-1';
            }else{                    
                $end = $year.'-'.($i+1).'-1';
            }
            $type0 .= model('users')->where(['dataFlag'=>1,'consume_type'=>['like','%0%']])->where('create_time','between time',[$start,$end])->count().',';
            $type1 .= model('users')->where(['dataFlag'=>1,'consume_type'=>['like','%1%']])->where('create_time','between time',[$start,$end])->count().',';
            $type2 .= model('users')->where(['dataFlag'=>1,'consume_type'=>['like','%2%']])->where('create_time','between time',[$start,$end])->count().',';
        }
        $this->assign("type0",$type0);
        $this->assign("type1",$type1);
        $this->assign("type2",$type2);
        return $this->fetch();
    }
}
